package cn.dnui_cr503.dnuifood_16110100503.model;

import cn.dnui_cr503.dnuifood_16110100503.bean.ResultBean;
import cn.dnui_cr503.dnuifood_16110100503.bean.UserBean;
import cn.dnui_cr503.dnuifood_16110100503.iface.UserIface;
import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by chengrui on 2018/10/12.
 */

public class UserModel extends BaseModel implements UserIface{
    private Retrofit retrofit;
    private String BaseUrl="http://172.24.10.175:8080/foodService/";
    private UserService service;
    public UserModel(){
        retrofit=new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
        service=retrofit.create(UserService.class);
    }

    @Override
    public void login(String username, String userpass, OnRetrofitListener listener) {
        Call call =service.doLogin(username,userpass);
        bindCallback(call,listener,Constants.USERLOGIN);
    }

    @Override
    public void reg(String username, String userpass, String mobilenum, String address, String comment, OnRetrofitListener listener) {
        Call call =service.doRegister(username,userpass,mobilenum,address,comment);
        bindCallback(call,listener,Constants.USERREGISTER);
    }

    public class Constants{
        public final static String BASE_URL="http://172.24.10.175:8080/foodService/";
        public final static int USERLOGIN=1;
        public final static int USERREGISTER=2;
        public final static int GETALLSHOPS=3;
    }
//    @Override
//    public void login(String username, String userpass, final OnRetrofitListener listener) {
//        Call<UserBean> call = service.doLogin(username,userpass);
//        call.enqueue(new Callback<UserBean>() {
//            @Override
//            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
//                if(response.body()!=null)
//                    listener.onSuccess(response.body(),1);
//                else
//                    listener.onFailure("解析有误！");
//            }
//
//            @Override
//            public void onFailure(Call<UserBean> call, Throwable t) {
//                listener.onFailure("联网异常！");
//            }
//        });
//    }
//
//    @Override
//    public void reg(String username, String userpass, String mobilenum, String address, String comment, final OnRetrofitListener listener) {
//        Call<ResultBean> call = service.doRegister(username,userpass,mobilenum,address,comment);
//        call.enqueue(new Callback<ResultBean>() {
//            @Override
//            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
//                if(response.body()!=null)
//                    listener.onSuccess(response.body(),1);
//                else
//                    listener.onFailure("解析有误！");
//            }
//            @Override
//            public void onFailure(Call<ResultBean> call, Throwable t) {listener.onFailure("联网异常！");}
//        });
//    }
}
